# mcp_service.py
import importlib
import yaml
from typing import Dict, Any

class EnhancedPyMasterControl:
    def __init__(self, config_path: str):
        self.config = self._load_config(config_path)
        self.modules = self._load_modules()
        self.service = None

    def _load_config(self, path: str) -> Dict[str, Any]:
        with open(path) as f:
            return yaml.safe_load(f)

    def _load_modules(self):
        """动态加载自定义模块"""
        modules = {}
        for mod_cfg in self.config.get('modules', []):
            module = importlib.import_module(mod_cfg['import_path'])
            cls = getattr(module, mod_cfg['class_name'])
            modules[mod_cfg['name']] = cls
        return modules

    def start_service(self):
        """根据配置启动服务"""
        service_config = self.config['service']
        self.service = self.modules[service_config['type']](**service_config['params'])
        self.service.start()

    def execute_command(self, command: str, **kwargs):
        """执行预定义命令"""
        if not self.service:
            raise RuntimeError("Service not started")
        
        cmd_config = self.config['commands'][command]
        handler = getattr(self.service, cmd_config['handler'])
        return handler(**kwargs)
